import csv
from django.shortcuts import render, redirect,HttpResponse
from .models import TeacherInfo
from django.db.models import Q
# Create your views here.
def index(request):
    data_dict1 = {}
    value = request.GET.get("q")

    if value:
        data_dict1["name__contains"] = value
    data_list = TeacherInfo.objects.filter(**data_dict1)
    return render(request, "index.html",{"data_list":data_list})

def result(request):
    data_dict1 = {}
    value = request.GET.get("q")

    if value:
        data_dict1["department__contains"] = value
    data_list = TeacherInfo.objects.filter(**data_dict1)
    return render(request, "result.html", {"data_list": data_list})

def orm(request):
    # TeacherInfo.objects.all().delete()
    csv_file = 'D:\学习资料\程序设计\python\\teacher\data1.csv'
    with open(csv_file, 'r', encoding='utf-8') as file:
        reader = csv.reader(file)
        # 跳过表头
        next(reader)
        i = 0
        for row in reader:
            i = i + 1
            TeacherInfo.objects.create(id=i,department=row[0],name=row[1],title=row[2],photo=row[3])
    return HttpResponse("上传成功")
def begin(request):
    return render(request, "begin.html")